float4x4 ViewProj : WORLDVIEWPROJ; //our world view projection matrix
float4x4 World : WORLD;
float4x4 WorldIT;

float3 LightDirection;
float4 xPlotArea = float4(-50,50,-50,50);

float3 SpecularColor = float3(1,1,1);
float SpecularIntensity = 1; 

bool UseSpecular = true;
bool DrawWhat = true;
float3 CameraPosition;
float  xAmbient;
texture xTexture;
sampler xTextureSampler:TEXUNIT2=
sampler_state
{
	Texture=<xTexture>;
	MipFilter=LINEAR;
	MinFilter=LINEAR;
	MagFilter=LINEAR;
};

//application to vertex structure
struct a2v
{
    float4 position : POSITION0;
    float3 normal: NORMAL0;
    float2 coord: TEXCOORD0;
};
//vertex to pixel processing structure
struct v2p
{
    float4 position : POSITION0;
    float2 Coords:TEXCOORD0;
	float3 Normal:TEXCOORD1;
	float4 Position3D : TEXCOORD2;
};

//pixel shader to screen
struct p2f
{
    float4 color : COLOR0;
};

//VERTEX SHADER
void vs( in a2v IN, out v2p OUT )
{
    //transforming our position from object space to screen space.
    float4x4 wvp = mul(World, ViewProj);
    OUT.position = mul(IN.position, wvp);
    // texure coordinates
    OUT.Coords = IN.coord;
    // word postition to ps
    OUT.Position3D = mul(IN.position, World);
    // normal vector 
    float3 Normal =normalize( mul( IN.normal, WorldIT));
    OUT.Normal = Normal;    
}

void ps( in v2p IN, out p2f OUT )
{
    //output some color
    OUT.color = tex2D(xTextureSampler, IN.Coords);
    // calculating specular
    float3 Light = normalize(LightDirection);
    float3 refl =normalize(-reflect(Light, normalize(IN.Normal)));
    float3 DirToCam = normalize(-CameraPosition+ IN.Position3D.xyz);
       
    if(UseSpecular)
    {
		// add specular light
		float3 spec =pow(dot( refl, DirToCam),256); 
		OUT.color.rgb += spec;
    }
     
    float Diffuse =saturate(dot( Light, normalize(IN.Normal)));
    
    OUT.color.rgb *= saturate(Diffuse + xAmbient);    
    
    // if pixel is out side plot area
    if( IN.Position3D.x < xPlotArea.x || IN.Position3D.x > xPlotArea.y ||
		IN.Position3D.z < xPlotArea.z || IN.Position3D.z > xPlotArea.w )
		{
			// grayscale
			float3 LuminanceConv = { 0.3125f, 0.8154f, 0.1721f };			
			OUT.color.rgb = dot(OUT.color.rgb, LuminanceConv);
			// alpha change								
			OUT.color.a =saturate(0.6 - saturate(sqrt(pow(IN.Position3D.x,2.0f)+ pow(IN.Position3D.z,2.0f))/ (50.0f)-1.0f));
			
			if(DrawWhat == true)
			{
				OUT.color = 0.0f;
			}			
		}
	else if(DrawWhat == false)
	{
		OUT.color = 0.0f;
	}
}
technique simple
{
    pass p0
    {		
        vertexshader = compile vs_2_0 vs();
		pixelshader = compile ps_2_0 ps();
    }
}